﻿using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.IO;
using XQ.Framework.core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;

namespace XQ.Framework.io.text
{
    public class JsonHelper
    {
        public static string ObjectToJson(object obj)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            JsonSerializer serializer = new JsonSerializer();
            System.IO.StringWriter sw = new System.IO.StringWriter();
            serializer.Serialize(new JsonTextWriter(sw), obj);
            return sw.GetStringBuilder().ToString();
        }
        public static JObject JsonToObject(string json)
        {
            if (!ValidateHelper.IsPlumpString(json))
            {
                return null;
            }
            return JObject.Parse(json);
        }
        public static T ParseJson<T>(string json)
        {
            if (!ValidateHelper.IsPlumpString(json)) { return default(T); }
            return JsonConvert.DeserializeObject<T>(json);
        }
        /// <summary>
        /// 简单的转换
        /// </summary>
        /// <param name="tb">表</param>
        /// <returns>json</returns>
        public static string DataTableToJson(DataTable dt)
        {
            if (dt == null || dt.Rows.Count == 0 || dt.Columns.Count == 0)
            {
                return string.Empty;
            }
            return JsonConvert.SerializeObject(dt, new DataTableConverter());
        }
        public static string DataTableToJson(DataTable dt, string dtName)
        {
            StringWriter sw = new StringWriter();
            using (JsonWriter jw = new JsonTextWriter(sw))
            {
                jw.WriteStartObject();
                jw.WritePropertyName(dtName);
                jw.WriteStartArray();

                for (int i = 0; i < dt.Rows.Count; ++i)
                {
                    jw.WriteStartObject();
                    for (int j = 0; j < dt.Columns.Count; ++j)
                    {
                        if (dt.Columns[j] == null || dt.Columns[j].ColumnName == null
                            || dt.Rows[i][j] == null) { continue; }
                        jw.WritePropertyName(dt.Columns[j].ColumnName);
                        jw.WriteValue(dt.Rows[i][j].ToString());
                    }
                    jw.WriteEndObject();
                }
                jw.WriteEndArray();
                jw.WriteEndObject();

                sw.Close();
                jw.Close();
            }
            return sw.GetStringBuilder().ToString();
        }
        public static string DictionaryToJson(Dictionary<string, string> dict, string dictName)
        {
            StringWriter sw = new StringWriter();
            using (JsonWriter jw = new JsonTextWriter(sw))
            {
                jw.WriteStartObject();
                jw.WritePropertyName(dictName);
                //////////////////////////////////////
                jw.WriteStartObject();
                List<string> keys = dict.Keys.ToList();
                keys.ForEach(delegate(string key)
                {
                    jw.WritePropertyName(key);
                    jw.WriteValue(dict[key]);
                });
                jw.WriteEndObject();
                //////////////////////////////////////
                jw.WriteEndObject();

                sw.Close();
                jw.Close();
            }
            return sw.GetStringBuilder().ToString();
        }
    }
}
